package com.oraro.paygatewaybiz.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oraro.common.result.Result;
import com.oraro.paygatewaybiz.module.entity.MchEntity;
import com.oraro.paygatewaybiz.module.service.MchService;
import com.oraro.paygatewaybiz.utils.ValidatorUtils;
import com.oraro.uidgeneratorcore.impl.DefaultUidGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: jianghuiyong
 * @Date: 2018/12/3 14:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/pay/mch")
public class MchController {

    @Autowired
    private MchService mchService;
    @Autowired
    private DefaultUidGenerator defaultUidGenerator;

    @GetMapping("/queryPage")
    public Result queryPage (Map<String, Object> params) {
        IPage<MchEntity> page = mchService.queryPage(params);
       return Result.success().data("page", page);
    }

    @GetMapping("/queryList")
    public Result queryList (Map<String, Object> params) {
        List<MchEntity> list = mchService.list(new QueryWrapper<>());
        return Result.success().data("list", list);
    }

    @GetMapping("/info/{mchId}")
    public Result info(@PathVariable Long mchId) {
        MchEntity mchEntity = mchService.selectByMchId(mchId);
        return Result.success().data("mch", mchEntity);
    }

    @PostMapping("/save")
    public Result save (@RequestBody MchEntity mchEntity) {
        mchEntity.setMchId(defaultUidGenerator.getUID());
        String mchKey = UUID.randomUUID().toString().replaceAll("-", "");
        mchEntity.setMchKey(mchKey);
        mchEntity.setDelFlag(0);
        mchEntity.setStatus(1);
        ValidatorUtils.validateEntity(mchEntity);
        mchService.save(mchEntity);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody MchEntity mchEntity) {
        ValidatorUtils.validateEntity(mchEntity);
        mchService.update(mchEntity);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result delete(Long[] mchId) {
        mchService.delete(mchId);
        return Result.success();
    }
}
